/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.msu.client.discovery;

import io.iec.edp.caf.commons.runtime.CafEnvironment;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.commons.utils.StringUtils;
import io.iec.edp.caf.msu.api.client.ServiceDiscovery;
import io.iec.edp.caf.msu.api.entity.MsuProperties;
import io.iec.edp.caf.msu.api.server.MsuServiceDiscovery;
import io.iec.edp.caf.msu.client.exception.ServiceUnitNotFoundException;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.core.env.Environment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 *  服务发现实现类
 *
 * @author Leon Huo
 */
@Slf4j
public class KubernetesDiscoveryImpl implements ServiceDiscovery {

    private static final int DEFAULT_PORT = 5200;
    private MsuProperties configuration;
    private boolean enableSSL = false;

    public KubernetesDiscoveryImpl(MsuProperties configuration) {
        this.configuration=configuration;
        this.enableSSL = this.enableSSL();
    }

    @Override
    public String discover(String serviceUnitName, HashMap<String, String> eventContext) {
        serviceUnitName = serviceUnitName.toLowerCase();
        String appName = configuration.getApplicationName();

        MsuServiceDiscovery msuServiceDiscovery = SpringBeanUtils.getBean(MsuServiceDiscovery.class);
        String result = msuServiceDiscovery.discover(appName,serviceUnitName);

        if (result == null) {
            throw new RuntimeException("k8s discover:current remote invoke can not found service by AppName:"+appName+" and SU:"+serviceUnitName);
        }

        return this.enableSSL? "https://":"http://" + result + ':' + DEFAULT_PORT;
    }


    @Override
    public List<String> discoverAll(String serviceUnitName, HashMap<String, String> eventContext) {
        //todo
        return new ArrayList<String>();
    }

    @Override
    public String discover(String namespace, String serviceUnitName, HashMap<String, String> eventContext) {
        return null;
    }

    @Override
    public List<String> discoverAll(String namespace, String serviceUnitName, HashMap<String, String> eventContext) {
        return null;
    }

    /**
     * @return 是否开启https
     */
    private boolean enableSSL() {
        Environment environment = CafEnvironment.getEnvironment();
        return !StringUtils.isEmpty(environment.getProperty("server.ssl.key-store"));
    }
}
